
#include <curses.h>
#include <stdlib.h>
#include <pthread.h>
#include <string.h>

#include "read_fan_temperature.h"
#include "../ec_it8733_inc/ec_it8733_dev.h"

#include "ec_it8733_rw.h"

//#define DBG_UART_PRT
//#include "dbg_prt.h"
#include "../ec_it8733_inc/dbg_prt.h"

#define ENTER 10
#define ESCAPE 27
static void init_curses()
{
        initscr();
        start_color();
        init_pair(1,COLOR_WHITE,COLOR_BLUE);
        init_pair(2,COLOR_BLUE,COLOR_WHITE);
        init_pair(3,COLOR_RED,COLOR_WHITE);
        curs_set(0);
        noecho();
        keypad(stdscr,TRUE);
}
static void draw_menubar(WINDOW *menubar)
{
        int i=0;
        int nMenuWidth=20;

        //wbkgd(menubar,COLOR_PAIR(2));

        wmove(menubar,0,nMenuWidth*i);
        waddstr(menubar,"read CHIPID");
        //wattron(menubar,COLOR_PAIR(3));
        waddstr(menubar,"(F3)");
        //wattroff(menubar,COLOR_PAIR(3));

        i++;
        wmove(menubar,0,nMenuWidth*i);
        //wmove(menubar,0,20);
        waddstr(menubar,"START");
        //wattron(menubar,COLOR_PAIR(3));
        waddstr(menubar,"(F4)");
        //wattroff(menubar,COLOR_PAIR(3));

        i++;
        wmove(menubar,0,nMenuWidth*i);
        waddstr(menubar,"EXIT");
        //wattron(menubar,COLOR_PAIR(3));
        waddstr(menubar,"(F9)");
        //wattroff(menubar,COLOR_PAIR(3));

        i=0;
        wmove(menubar,1,nMenuWidth*i);
        waddstr(menubar,"PWN&FAN");
        //wattron(menubar,COLOR_PAIR(3));
        waddstr(menubar,"(UP)");
        //wattroff(menubar,COLOR_PAIR(3));

        i++;
        wmove(menubar,1,nMenuWidth*i);
        waddstr(menubar,"PWN&FAN");
        //wattron(menubar,COLOR_PAIR(3));
        waddstr(menubar,"(DOWN)");
        //wattroff(menubar,COLOR_PAIR(3));


}
static WINDOW **draw_menu(int start_col)
{
        int i;
        WINDOW **items;
        items=(WINDOW **)malloc(9*sizeof(WINDOW *));

        items[0]=newwin(10,19,1,start_col);
        wbkgd(items[0],COLOR_PAIR(2));
        box(items[0],ACS_VLINE,ACS_HLINE);
        items[1]=subwin(items[0],1,17,2,start_col+1);
        items[2]=subwin(items[0],1,17,3,start_col+1);
        items[3]=subwin(items[0],1,17,4,start_col+1);
        items[4]=subwin(items[0],1,17,5,start_col+1);
        items[5]=subwin(items[0],1,17,6,start_col+1);
        items[6]=subwin(items[0],1,17,7,start_col+1);
        items[7]=subwin(items[0],1,17,8,start_col+1);
        items[8]=subwin(items[0],1,17,9,start_col+1);
        for (i=1;i<9;i++)
                wprintw(items[i],"Item%d",i);
        wbkgd(items[1],COLOR_PAIR(1));
        wrefresh(items[0]);
        return items;
}
static void delete_menu(WINDOW **items,int count)
{
        int i;
        for (i=0;i<count;i++)
                delwin(items[i]);
        free(items);
}
static int scroll_menu(WINDOW **items,int count,int menu_start_col)
{
        int key;
        int selected=0;
        while (1) {
                key=getch();
                if (key==KEY_DOWN || key==KEY_UP) {
                        wbkgd(items[selected+1],COLOR_PAIR(2));
                        wnoutrefresh(items[selected+1]);
                        if (key==KEY_DOWN) {
                                selected=(selected+1) % count;
                        } else {
                                selected=(selected+count-1) % count;
                        }
                        wbkgd(items[selected+1],COLOR_PAIR(1));
                        wnoutrefresh(items[selected+1]);
                        doupdate();
                } else if (key==KEY_LEFT || key==KEY_RIGHT) {
                        delete_menu(items,count+1);
                        touchwin(stdscr);
                        refresh();
                        items=draw_menu(20-menu_start_col);
                        return scroll_menu(items,8,20-menu_start_col);
                } else if (key==ESCAPE) {
                        return -1;
                } else if (key==ENTER) {
                        return selected;
                }
        }
}


extern EN_RUN_STATE_TYPE en_run_state;
extern EN_KEY_VALUE_TYPE en_key_value;
void *thr_fn(void *arg);

static WINDOW *msgBoxCpuTemperature;
static WINDOW *msgBoxMainBoardTemperature;
static WINDOW *msgChassisBoardTemperature;
static WINDOW *msgFanRevolution;
static WINDOW *msgFanPwm;

static WINDOW *messagebar;

void disp_cpu_temperature(int nval)
{
        mvwprintw(msgBoxCpuTemperature,1,30,"%10d",nval);
        touchwin(stdscr);
        refresh();
}

void disp_main_board_temperature(int nval)
{
        mvwprintw(msgBoxMainBoardTemperature,1,30,"%10d",nval);
        touchwin(stdscr);
        refresh();
}

void disp_chassis_temperature(int nval)
{
        mvwprintw(msgChassisBoardTemperature,1,30,"%10d",nval);
        touchwin(stdscr);
        refresh();
}

void disp_cpu_fan(int nval)
{
        mvwprintw(msgFanRevolution,1,30,"%10d",nval);
        touchwin(stdscr);
        refresh();
}

void disp_fan_pwm(int nval)
{
        mvwprintw(msgFanPwm,1,30,"%10d",nval);
        touchwin(stdscr);
        refresh();
}

void disp_sleep_msg(char* str)
{
        mvwprintw(messagebar,4,2,"%s",str);
        touchwin(stdscr);
        refresh();
}

//static unsigned char g_u8FanPwm=128;
#if 0
unsigned char  getFanPwmBox(void)
{
        return g_u8FanPwm;
}
#endif


static void create_disp_box()
{
    int nboxwidth=3;
    int nboxheight=50;
    int nboxstarty=4;
    int nboxstartx=1;
    int nboxgap=3;

    //msgBoxCpuTemperature=subwin(stdscr,3,79,10,1);
    msgBoxCpuTemperature=subwin(stdscr,nboxwidth,nboxheight,nboxstarty,nboxstartx);
    box(msgBoxCpuTemperature,ACS_VLINE,ACS_HLINE);
    mvwprintw(msgBoxCpuTemperature,1,2,"cpu temperature: ");

    nboxstarty+=nboxgap;
    msgBoxMainBoardTemperature=subwin(stdscr,nboxwidth,nboxheight,nboxstarty,nboxstartx);
    //msgBoxMainBoardTemperature=subwin(stdscr,3,79,15,1);
    box(msgBoxMainBoardTemperature,ACS_VLINE,ACS_HLINE);
    mvwprintw(msgBoxMainBoardTemperature,1,2,"main board temperature: ");

    nboxstarty+=nboxgap;
    msgChassisBoardTemperature=subwin(stdscr,nboxwidth,nboxheight,nboxstarty,nboxstartx);
    //msgChassisBoardTemperature=subwin(stdscr,3,79,20,1);
    box(msgChassisBoardTemperature,ACS_VLINE,ACS_HLINE);
    mvwprintw(msgChassisBoardTemperature,1,2,"chasis temperature: ");

    nboxstarty+=nboxgap;
    msgFanPwm=subwin(stdscr,nboxwidth,nboxheight,nboxstarty,nboxstartx);
    //msgFanPwm=subwin(stdscr,3,79,25,1);
    box(msgFanPwm,ACS_VLINE,ACS_HLINE);
    mvwprintw(msgFanPwm,1,2,"Fan Pwm Control: ");
    //mvwprintw(msgFanPwm,1,30,"%10d",g_u8FanPwm);
    //                    touchwin(stdscr);
    //                    refresh();   

    nboxstarty+=nboxgap;
    msgFanRevolution=subwin(stdscr,nboxwidth,nboxheight,nboxstarty,nboxstartx);
    //msgFanRevolution=subwin(stdscr,3,79,30,1);
    box(msgFanRevolution,ACS_VLINE,ACS_HLINE);
    mvwprintw(msgFanRevolution,1,2,"fan revolution: ");

    nboxstarty+=nboxgap;
    messagebar=subwin(stdscr,10,nboxheight,nboxstarty,1);
    //messagebar=subwin(stdscr,10,79,35,1);
    box(messagebar,ACS_VLINE,ACS_HLINE);
    mvwprintw(messagebar,2,2,"msg: ");

}


int win_stdscr()
{
    int ch;
    WINDOW *menubar;

    init_curses();

    //bkgd(COLOR_PAIR(1));

    menubar=subwin(stdscr,2,80,0,0);

    create_disp_box();

    draw_menubar(menubar);
    //move(2,1);
    //printw("Press F1 or F2 to open the menus. ");
    //printw("ESC quits.");
    refresh();

    do
    {

        ch=getch();

        //dbg_prt( "en_run_state: %4d; ch=%d", en_run_state, ch );
        dbg_prt( "en_run_state: %4d; ch=%d", en_run_state, ch );
        mvwprintw(messagebar,2,10,"en_run_state: %4d; ch=%d", en_run_state, ch);

        //if(ch >= defKeyFMenuBgn  && ch <= defKeyFMenuBgn+5 )
        {
                if(ch == KEY_F(3))
                {
                        unsigned char chipid0;
                        unsigned char chipid1;
                        CEcIt8733Rw::getInstance()->get_chip_id(&chipid0, &chipid1);
                        dbg_prt( "chipid0 =%02X; chipid1 =%02X",chipid0, chipid1 );
                        mvwprintw(messagebar,3,2,"chipid0 =%02X; chipid1 =%02X",chipid0, chipid1);

                }

                #if 0
                else if (ch == KEY_F(4))
                {
                        if(en_run_state!=RUN_STATE_START)
                        {
                                int err;
                                pthread_t ntid;
                                err = pthread_create(&ntid, NULL, thr_fn, NULL);
                                if(err != 0)
                                {
                                        en_run_state=RUN_STATE_EXIT;
                                        dbg_prt( "can't create thread: %s ", strerror(err) );
                                        mvwprintw(messagebar,3,2,"can't create thread: %s ", strerror(err));
                                }
                                else
                                {
                                        en_run_state=RUN_STATE_START;
                                        dbg_prt( "en_run_state: %4d ", en_run_state );
                                        mvwprintw(messagebar,3,2,"line =%d; en_run_state: %4d ",__LINE__, en_run_state);

                                }
                        }

                }
                
                else if (ch == KEY_F(5))
                {
                        unsigned char u8FanPwmLocal;
                        int nCpuTemp; int nMainBoardTemp; int nChassisTemp;
                        int nCpuFan;
                        ec_it8733_start_read_cpu_temp( &u8FanPwmLocal, &nCpuTemp, &nMainBoardTemp, &nChassisTemp, &nCpuFan );

				disp_cpu_temperature(nCpuTemp);
				disp_main_board_temperature(nMainBoardTemp);
				disp_chassis_temperature(nChassisTemp);
				disp_cpu_fan(nCpuFan);   
                                disp_fan_pwm(u8FanPwmLocal);                     


                        #if 0
                        if(en_run_state!=RUN_STATE_START)
                        {
                                void entry_test_status(void);
                                //entry_test_status();
                                int err;
                                pthread_t ntid;
                                err = pthread_create(&ntid, NULL, thr_fn, NULL);
                                if(err != 0)
                                {
                                        en_run_state=RUN_STATE_EXIT;
                                        dbg_prt( "can't create thread: %s ", strerror(err) );
                                }
                                else
                                {
                                        en_run_state=RUN_STATE_START;
                                        dbg_prt( "en_run_state: %4d ", en_run_state );
                                }
                        }
                        #endif

                }     
                #endif

                #if 1
                else if (ch == KEY_F(4))
                {

                        {
                                unsigned char u8FanPwmLocal;
                                int nCpuTemp; int nMainBoardTemp; int nChassisTemp;
                                int nCpuFan;
                                CEcIt8733Rw::getInstance()->ec_it8733_start_read_cpu_temp( &u8FanPwmLocal, &nCpuTemp, &nMainBoardTemp, &nChassisTemp, &nCpuFan );

                                        disp_cpu_temperature(nCpuTemp);
                                        disp_main_board_temperature(nMainBoardTemp);
                                        disp_chassis_temperature(nChassisTemp);
                                        disp_cpu_fan(nCpuFan);   
                                        disp_fan_pwm(u8FanPwmLocal);                     


                        }     


                        if(en_run_state!=RUN_STATE_START)
                        {
                                int err;
                                pthread_t ntid;
                                err = pthread_create(&ntid, NULL, thr_fn, NULL);
                                if(err != 0)
                                {
                                        en_run_state=RUN_STATE_EXIT;
                                        dbg_prt( "can't create thread: %s ", strerror(err) );
                                        mvwprintw(messagebar,3,2,"can't create thread: %s ", strerror(err));
                                }
                                else
                                {
                                        en_run_state=RUN_STATE_START;
                                        dbg_prt( "en_run_state: %4d ", en_run_state );
                                        mvwprintw(messagebar,3,2,"line =%d; en_run_state: %4d ",__LINE__, en_run_state);

                                }
                        }



                }
                
                
                #endif


                #if 0
                else if (ch == KEY_F(6))
                {
                        unsigned char u8FanPwm;
                        ssize_t ec_it8733_get_fan_pwm(unsigned char *u8FanPwm );
                        ec_it8733_get_fan_pwm(&u8FanPwm );

                        dbg_prt( "en_run_state: %4d ;u8FanPwm =%10d", en_run_state , u8FanPwm );
                }		

                else if (ch == KEY_F(7))
                {
                        unsigned char u8FanPwm = 13;
                        ssize_t ec_it8733_set_fan_pwm(unsigned char u8FanPwm );
                        ec_it8733_set_fan_pwm(u8FanPwm );

                        dbg_prt( "en_run_state: %4d ;u8FanPwm =%10d", en_run_state , u8FanPwm );
                }	
                #endif                

                else if (ch == KEY_F(9))
                {

                        en_run_state=RUN_STATE_EXIT;
                        dbg_prt( "en_run_state: %4d ", en_run_state );
                        mvwprintw(messagebar,3,2,"en_run_state: %4d ", en_run_state);
                }

                else if (ch==KEY_DOWN || ch==KEY_UP)
                {
                        unsigned char u8FanPwm;
                        
                        CEcIt8733Rw::getInstance()->ec_it8733_get_fan_pwm(&u8FanPwm );                        
                        
                        if (ch==KEY_UP)
                        {
                                u8FanPwm++;
                                /*
                                g_u8FanPwm++;
                                //g_u8FanPwm>=256)
                                  //      g_nFanPwm=256;
                                  en_key_value = RUN_STATE_ARROW_UP;
                                */
                        }
                        else
                        {

                                u8FanPwm--;




#if 0                                
                                g_u8FanPwm--;
                                //if(g_nFanPwm<=0)
                                  //      g_nFanPwm=0;
                                  en_key_value = RUN_STATE_ARROW_DOWN;
#endif

                        }

                        
                        CEcIt8733Rw::getInstance()->ec_it8733_set_fan_pwm(u8FanPwm );


                        {
                                unsigned char u8FanPwmLocal;
                                                        int nCpuTemp; int nMainBoardTemp; int nChassisTemp;
                                int nCpuFan;
                                CEcIt8733Rw::getInstance()->ec_it8733_read_cpu_temp( &u8FanPwmLocal, &nCpuTemp, &nMainBoardTemp, &nChassisTemp, &nCpuFan );

                        disp_cpu_temperature(nCpuTemp);
                        disp_main_board_temperature(nMainBoardTemp);
                        disp_chassis_temperature(nChassisTemp);
                        disp_cpu_fan(nCpuFan);   
                        disp_fan_pwm(u8FanPwmLocal);                     


                        }


                        mvwprintw(msgFanPwm,1,30,"%10d",u8FanPwm);
                        touchwin(stdscr);
                        refresh();                        
                        

                }



        }
        
        touchwin(stdscr);
        //werase(messagebar);
        wrefresh(messagebar);

        refresh();

        #if 0
        int selected_item;
        WINDOW **menu_items;
        key=getch();
        werase(messagebar);
        wrefresh(messagebar);
        if (key==KEY_F(3)) 
        {
            menu_items=draw_menu(0);
            selected_item=scroll_menu(menu_items,8,0);
            delete_menu(menu_items,9);
            if (selected_item<0)
                wprintw(messagebar,"You haven't selected any item.");
            else
                wprintw(messagebar,
                  "You have selected menu item %d.",selected_item+1);
            touchwin(stdscr);
            refresh();
        } 
        else if (key==KEY_F(4)) 
        {
            menu_items=draw_menu(20);
            selected_item=scroll_menu(menu_items,8,20);
            delete_menu(menu_items,9);
            if (selected_item<0)
                wprintw(messagebar,"You haven't selected any item.");
            else
                wprintw(messagebar,
                  "You have selected menu item %d.",selected_item+1);
            touchwin(stdscr);
            refresh();
        }
        #endif


    } while (ch!=KEY_F(9));

    delwin(menubar);
    delwin(messagebar);
    endwin();
    return 0;
}


